from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from utils.driver_aolai import Driver
import time,logging
from selenium.common.exceptions import TimeoutException


class BaseObject:
    def __init__(self):
        self.driver = Driver.get_app_driver()

    def search_ele(self, loc, timeout=5, poll_frequency=1.0):
        logging.info("操作元素:{}".format(loc))
        # WebDriverWait(self.driver,timeout,poll).until(lambda x:x.find_element(ty,value))
        return WebDriverWait(self.driver, timeout, poll_frequency).until(lambda x: x.find_element(loc[0], loc[1]))

    def search_eles(self, loc, timeout=5, poll_frequency=1.0):
        # WebDriverWait(self.driver,timeout,poll).until(lambda x:x.find_element(ty,value))
        return WebDriverWait(self.driver, timeout, poll_frequency).until(lambda x: x.find_elements(loc[0], loc[1]))


class BaseHandle:
    def input(self, ele, value):
        ele.clear()
        ele.send_keys(value)

    def move_all(self, tag=1):
        driver = Driver.get_app_driver()
        size = driver.get_window_size()
        width = size.get("width")
        height = size.get("height")
        if tag == 1:
            logging.info("向上移动")
            driver.swipe(width * 0.5, height * 0.8, width * 0.5, height * 0.2, 1500)
        if tag == 2:
            logging.info("向下移动")
            driver.swipe(width * 0.5, height * 0.2, width * 0.5, height * 0.8, 1500)
        if tag == 3:
            logging.info("向左移动")
            driver.swipe(width * 0.2, height * 0.5, width * 0.8, height * 0.5, 1500)
        if tag == 4:
            logging.info("向右移动")
            driver.swipe(width * 0.8, height * 0.5, width * 0.2, height * 0.5, 1500)

    def toast_message(self, mess, tag="a"):
        if tag == "a":
            self.mes = (By.XPATH, "//*[contains(@text,'{}')]".format(mess))
        if tag == "w":
            self.mes = (By.XPATH, "//*[contains(text(),'{}')]".format(mess))
        try:
            logging.info("{}存在".format(mess))
            BaseObject().search_ele(self.mes, 3, 0.3)
            return True
        except TimeoutException:
            logging.info("{}不存在".format(mess))
            return False


if __name__ == '__main__':
    b = BaseObject()
    a = b.search_ele((By.ID, 'com.android.settings:id/search'))
    a.click()
    time.sleep(2)
    c = b.search_ele((By.ID, 'android:id/search_src_text'))
    d = BaseHandle()
    time.sleep(1)
    d.input(c, 1)
